package org.corey.dev.voxiverse;


import org.corey.dev.voxiverse.universe.Universe;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class Game {
	private Universe universe;
	public static Player player;
	
	
	public static void main(String[] args) throws LWJGLException{
		//System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/natives/");
		//uncomment the above when packaging and delete throws exception
		Game r = new Game();
		r.Start();
	}
	
	private void Run(){
		System.out.println("Building Universe...");
		universe = new Universe();
		
		player = new Player(-40f, -682f, -40f);
		
		Mouse.setGrabbed(true);
		
		while (!Display.isCloseRequested()&&!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
			try{
				InputHandler.ProcessInput();
			
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
				GL11.glLoadIdentity();
				
				//System.out.println(player.PlayerX +","+player.PlayerY+","+player.PlayerZ+"   "+(player.ChunkX / (Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH)) +","+ (player.ChunkZ / (Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH)));
				universe.Update();
				universe.Render();
			
			
				Display.update();
				Display.sync(60);
			
			}	catch (Exception e){
				Mouse.setGrabbed(false);
				System.out.println("The Exception " + e + " was caught.");
			}
		}
		
		Mouse.setGrabbed(false);
		
		
		Display.destroy();
	}
	
	
	public void Start(){
		try{
			CreateWindow();
			InitGL();
			
			Run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
DisplayMode displayMode;
	
	private void CreateWindow() throws Exception {
		Display.setFullscreen(false);
		DisplayMode d[] = Display.getAvailableDisplayModes();
		for (int i = 0; i < d.length; i++) {
			if (d[i].getWidth() == 800 && d[i].getHeight() == 600
					&& d[i].getBitsPerPixel() == 32) {
				displayMode = d[i];
				break;
			}
		}
		Display.setDisplayMode(displayMode);
		Display.setTitle("LWJGL COMP4");
		Display.create();
	}
	
	private void InitGL() {
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClearDepth(1.0);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_LEQUAL);
		GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

		GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();

		GLU.gluPerspective(45.0f, (float) displayMode.getWidth()
				/ (float) displayMode.getHeight(), 0.1f, 300.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);

		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
	}
	
	
	
	public enum GameState {
		State_Menu(0), State_SinglePlayer(1), State_Multiplayer(2), State_Editor(
				3);
		private int StateID;

		GameState(int i) {
			StateID = i;
		}

		public int GetID() {
			return StateID;
		}
	}
}
